#include "server.h"
#include "database.h"

//定义一个1024个元素的事件结构体数组
struct epoll_event evtArr[MES_BUF_SIZE];
//定义一个用来储存用户fd id的数组
int USERID[MES_BUF_SIZE];
//使用量
int used=0;
//定义一个账号以及fd结构体数组
ONLINE line[MES_BUF_SIZE];
//登录在线量
int onUsed=0;


//聊天在线
ONLINE lineChat[MES_BUF_SIZE];
//聊天在线人数
int lineSize=0;

//监听套接字
int listenFd;

//初始化监听套接字
int initTcpSocket()      
{
    unsigned int ip;
    //把字符串IP转为网络（大端）字节序
    inet_pton(AF_INET,ID,&ip);
    struct sockaddr_in addr;
    addr.sin_family = AF_INET;//IPV4地址族
    addr.sin_port = htons(PORT);//转成网络字节序的端口号
    addr.sin_addr.s_addr = ip;//网络字节序IP地址

    // 如果要通信，就需要创建socket，返回SOCKET的文件描述符
    //参数1：地址族IPV4
    //参数2：流式SOCKET(tcp协议)
    //参数3：自动网络协议
    int fd = socket(AF_INET,SOCK_STREAM,0);

    int opt = 1;
    //允许fd这个套接字去绑定一个还没有被释放的端口地址，当强行关闭服务器之后，马上又开启服务器时，使我们的套接字能够顺利的绑定上次绑定的端口号
    if(setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
    {
        printf("设置允许socket可以重复绑定地址失败\n");
    }

    //绑定电脑上的一个端口
    //参数1：socket
    //参数2：网络地址，为了满足历史遗留问题，需要把地址转为struct sockaddr *
    //参数3：结构体的长度
    bind(fd,(struct sockaddr *)&addr,sizeof(addr));

    //启动监听，监听有没有人连链接我，最多允许5个客户端同时排队连接，超过的直接返回连接错误
    listen(fd,5);
    return fd;
}

//创建并初始化epoll对象
int createInitEpoll(void)       
{
    listenFd = initTcpSocket();
    //epoll类似于select，是帮助我们检测套接字的
    //创建一个epoll对象，参数为epoll对象要管理的套接字数量
    int tmpep = epoll_create(MES_BUF_SIZE);

    //定义一个事件结构体，里面需要填写我们关心的事件，例如可读事件/可写事件等。。。
    struct epoll_event ee;
    ee.events = EPOLLIN;//填写我们关心的事件，EPOLLIN是可读事件
    ee.data.fd = listenFd;//填写需要关心可读事件的套接字，这里填写的是监听套接字
    //把监听套接字和它对应的事件结构体加入到EPOLL对象中进行管理（检测）
    epoll_ctl(tmpep,EPOLL_CTL_ADD,listenFd,&ee);

    return tmpep;
}

//对用户fd进行处理，1添加，0删除
void manageFd(int tmpFd,int epObj,int n)        
{
    if(n==0)
    {
        close(tmpFd);

        struct epoll_event ee;
        ee.data.fd=tmpFd;
        ee.events=EPOLLIN;
        epoll_ctl(epObj,EPOLL_CTL_DEL,tmpFd,&ee);
        //fdid数组删除
        for(int i=0;i<used;i++)
        {
            if(USERID[i]==tmpFd)
            {
                for(int j=i;j<used;j++)
                {
                    USERID[j]=USERID[j+1];
                }
                used--;
            }
        }
        //判断是否在线，在线即删除
        for(int i=0;i<onUsed;i++)
        {
            if(line[i].fd==tmpFd)
            {
                for(int j=i;j<onUsed;j++)
                {
                    line[j]=line[j+1];
                }
                onUsed--;              
            }
        }

    }
    else if(n==1)
    {
        //用来存放客户端的地址
        struct sockaddr_in client_addr;
        //客户端的地址长度
        socklen_t len = sizeof(client_addr);
        //接受客户端的连接并返回新的用来和客户端通信的套接字
        //因为fdNew是一个通信套接字，它上面也有可能产生可读事件。例如：客户端发消息过来
        //所以也需要把它放到集合里让select监控
        int fdNew = accept(listenFd,(struct sockaddr *)&client_addr,&len);
        USERID[used++]=fdNew;
        //定义一个事件结构体，里面需要填写我们关心的事件，例如可读事件/可写事件等。。。
        struct epoll_event ee;
        //添加用户id
        ee.events=EPOLLIN;
        //填写需要关心可读事件的套接字，这里填写的是监听套接字
        ee.data.fd = fdNew;
        //把监听套接字和它对应的事件结构体加入到EPOLL对象中进行管理（检测）
        epoll_ctl(epObj,EPOLL_CTL_ADD,fdNew,&ee);
    }

}

//将消息发给tmpFd以外的人
void sendMessage(int tmpFd,char *buf)  
{
    printf("%s\n",buf);
    for(int i=0;i<used;i++)
    {
        if(USERID[i]!=tmpFd){
            write(USERID[i],buf,sizeof(buf));
        }
    }
}

//有相同发送yes,无相同发送no
int yorn(BUF *tmp,int fd,int row)
{
    if(row==0)
    {
        tmp->choice=NO;
        write(fd,tmp,sizeof(BUF));   
        return 0;                         
    }
    else
    {
        tmp->choice=YES;
        write(fd,tmp,sizeof(BUF));
        return 1;
    }
}

//账号生成函数
void AccountGeneratingFunction(char *zh)
{
    FILE *ffd=fopen("zhanghao.txt","ab+");
    if(fgetc(ffd)==EOF)
    {
        strcpy(zh,"100000");
        fwrite(zh,sizeof(char)*20,1,ffd);
    }
    else
    {
        fseek(ffd,-20,SEEK_END);
        fread(zh,sizeof(char)*20,1,ffd);
        int tmpMath=atoi(zh);
        tmpMath++;
        memset(zh,0,20);
        sprintf(zh,"%d",tmpMath);
        fwrite(zh,sizeof(char)*20,1,ffd);
    }
    fclose(ffd);
}

//根据fd查找在线账号
void lineAccountText(ONLINE *line,int usage,char *account,int fd)
{
    for(int i=0;i<usage;i++)
    {
        if(line[i].fd==fd)
        {
            strcpy(account,line[i].zh);
            break;
        }
    }
}

//接收信息并处理函数
void eopllWait(int epObj)      
{
    while (1)
    {
        //epoll_wait等待epObj所管理的套接字上发生了感兴趣事件，如果某个套接字发生了对应的事件，epoll_wait会把
        //该套接字对应的事件结构体存入到第二个参数的数组中
        //返回值：返回当前epoll对象中有多少个fd发生了可读事件，也就此时有多少个fd对应的结构体被计入到了第二个参数的数组中
        int ret = epoll_wait(epObj,evtArr,sizeof(evtArr),-1);

        for (size_t i = 0; i < ret; i++)
        {
            if(evtArr[i].data.fd == listenFd)//如果套接字等于监听套接字，需要接受客户端链接
            {
                manageFd(0,epObj,1);             //对新连接fd进行添加处理
                printf("有用户链接上来了\n");
            }
            //非接连即单独用户处理
            else
            {
                char buf[MES_BUF_SIZE];
                //read函数会阻塞等待消息的到来
                int ret=read(evtArr[i].data.fd,buf,sizeof(buf));
                if(ret==0)//接收数据为零证明用户退出
                {
                    manageFd(evtArr[i].data.fd,epObj,0);    //对退出的用户fd进行删除处理
                }
                else if(ret==-1)
                {
                    printf("出错\n");
                }
                else
                {
                    BUF *tmpbuf=(BUF*)buf;
                    USERMES *usermes=(USERMES*)tmpbuf->buf;
                    if(tmpbuf->choice==INQUIRE)//手机号查重判断
                    {
                        char sql[256];
                        sprintf(sql,"select PhoneNumber from user where PhoneNumber='%s'",usermes->PhoneNumber);
                        QUERYRESULT *qwer = query_db(pDB,sql);
                        yorn(tmpbuf,evtArr[i].data.fd,qwer->row);
                    }
                    else if(tmpbuf->choice==REGISTER)//为唯一的手机号生成账号(手机验证通过后生成账号即注册)
                    {
                        memset(usermes->AccountNumber,0,20);
                          //生成账号
                        AccountGeneratingFunction(usermes->AccountNumber);
                        char sql[256];
                        //创建用户列表
                        sprintf(sql,"INSERT INTO user (PhoneNumber,UserName,RetrieveQuestion,QuestionAnswer,AccountNumber,Password) VALUES ('%s','%s','%s','%s','%s','%s')",usermes->PhoneNumber,usermes->UserName,
                        usermes->RetrieveQuestion,usermes->QuestionAnswer,usermes->AccountNumber,usermes->Password);
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("插入数据成功\n");
                        }                                            
                        write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                        memset(sql,0,sizeof(sql));
                        //创建用户朋友列表
                        sprintf(sql,"create table ACCNUM_%s_friend(FRIEND_AccountNumber, Time timestamp not null default (datetime('now','localtime')))",usermes->AccountNumber);
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("账号%s朋友列表创建成功。\n",usermes->AccountNumber);
                        }
                        memset(sql,0,sizeof(sql));
                        //创建用户消息列表
                        sprintf(sql,"create table ACCNUM_%s_MES(seceiver,sender,content,message_type,message_state, Time timestamp not null default (datetime('now','localtime')))",usermes->AccountNumber);
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("账号%s消息列表创建成功。\n",usermes->AccountNumber);
                        }
                    }
                    else if(tmpbuf->choice==LOGIN)//判断账号密码是否正确(登录)
                    {
                        char sql[256];
                        sprintf(sql,"select AccountNumber Password from user where AccountNumber='%s' and Password='%s'",usermes->AccountNumber,usermes->Password);
                        QUERYRESULT *qwer = query_db(pDB,sql);
                        int ret=yorn(tmpbuf,evtArr[i].data.fd,qwer->row);
                        printf("%s登录\n",usermes->AccountNumber);
                        if(ret==1)
                        {
                            ONLINE tmpLine;
                            tmpLine.fd=evtArr[i].data.fd;
                            strcpy(tmpLine.zh,usermes->AccountNumber);
                            line[onUsed++]=tmpLine;
                        }
                    }
                    else if(tmpbuf->choice==FINDACCOUNT)//判断账号(添加好友，找回密码)
                    {
                        char sql[256];
                        sprintf(sql,"select AccountNumber from user where AccountNumber='%s'",tmpbuf->buf);  
                        QUERYRESULT *qwer = query_db(pDB,sql);
                        ret=yorn(tmpbuf,evtArr[i].data.fd,qwer->row);                      
                    }
                    else if(tmpbuf->choice==ADDFREIND)//添加好友
                    {
                        char presentNumber[20];
                        //获取用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        char sql[256];
                        sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','NULL','%s','%s')",tmpbuf->buf,tmpbuf->buf,presentNumber,REQUEST,PEND);                         
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("添加好友信息已经发送。\n");
                        }   
                    }
                    else if(tmpbuf->choice==ALLMES)//信息更新(每次刷新时调用)
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        char sql[256];
                        //定义一个存放int的结构体用来传送给客户端接收到多少信息，不想试直接发送一个数字
                        COUNT shu;
                        //------------------------------------------------------检测未读消息的，接收者为自己，消息类型为消息，状态为未读------------------------------------
                        sprintf(sql,"select sender,content from ACCNUM_%s_MES where seceiver='%s' and message_type='%s' and message_state='%s' order by Time",presentNumber,presentNumber,INFORM,UNREAD);
                        QUERYRESULT *asdf = query_db(pDB,sql);
                        if(asdf->row==0)
                        {
                            //没读到给用户返回一个0
                            shu.quantity=0;
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                        }
                        else
                        {
                            int y=asdf->row;
                            shu.quantity=y;
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                            for(int w=0;w<(2*y+2);w+=2)
                            {
                                //第0个和第1个为列名sender和content应跳过
                                if(w!=0&&w!=1)
                                {
                                    memset(sql,0,sizeof(char)*256);
                                    sprintf(sql,"%s:%s。",asdf->ppResult[w],asdf->ppResult[w+1]);
                                    strcpy(tmpbuf->buf,sql);
                                    write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                                }
                            } 
                        }
                        memset(sql,0,sizeof(char)*256);
                        //----------------------------------------检测未处理好友申请的，消息类型为好友申请，状态为待处理-------------------------------------------------------------
                        sprintf(sql,"select sender from ACCNUM_%s_MES where message_type='%s' and message_state='%s' order by Time",presentNumber,REQUEST,PEND);
                        QUERYRESULT *qwer = query_db(pDB,sql);
                        if(qwer->row==0)
                        {
                            //没读到给用户返回一个0
                            shu.quantity=0;  
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                        }
                        else
                        {
                            int x=qwer->row;
                            //printf("一共有%d条记录%d\n",qwer->row,qwer->col);
                            shu.quantity=x;
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                            for(int w=0;w<(x+1);w++)
                            {
                                //第0个为列名sender，应跳过
                                if(w!=0)
                                {
                                    memset(sql,0,sizeof(char)*256);
                                    sprintf(sql,"%s",qwer->ppResult[w]);  //发送账号
                                    strcpy(tmpbuf->buf,sql);
                                    write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                                }
                            }
                        }
                        memset(sql,0,sizeof(char)*256);
                        //----------------------------------------------------用户好友列表更新-----------------------------------------------------------------
                        sprintf(sql,"select FRIEND_AccountNumber from ACCNUM_%s_friend order by Time",presentNumber);
                        QUERYRESULT *zxcv = query_db(pDB,sql);
                        if(zxcv->row==0)
                        {
                            shu.quantity=0;
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                        }
                        else
                        {
                            int z=zxcv->row;
                            shu.quantity=z;
                            memcpy(tmpbuf->buf,&shu,sizeof(COUNT));
                            //告诉客户端有几个数据同步循环次数
                            write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                            for(int w=0;w<(z+1);w++)
                            {
                                //第0个是FRIEND_AccountNumber，跳过
                                if(w!=0)
                                {
                                    memset(sql,0,sizeof(char)*256);
                                    //发送好友账号
                                    sprintf(sql,"%s",zxcv->ppResult[w]);
                                    strcpy(tmpbuf->buf,sql);
                                    write(evtArr[i].data.fd,tmpbuf,sizeof(BUF));
                                }
                            }                            
                        } 
                    }
                    else if(tmpbuf->choice==YES)//用户同意添加好友
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        char sql[256];
                        //当前账号同意申请
                        sprintf(sql,"update ACCNUM_%s_MES set message_state='%s' where sender='%s' and message_type='%s'",presentNumber,PRO,tmpbuf->buf,REQUEST);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("消息已改为同意。\n");
                        }   
                        memset(sql,0,sizeof(char)*256);    
                        //对方接收到同意
                        sprintf(sql,"update ACCNUM_%s_MES set message_state='%s' where sender='%s' and message_type='%s'",tmpbuf->buf,PRO,tmpbuf->buf,REQUEST);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("消息已改为同意。\n");
                        } 
                        memset(sql,0,256);
                        //好友添加
                        sprintf(sql,"INSERT INTO ACCNUM_%s_friend(FRIEND_AccountNumber) VALUES ('%s')",tmpbuf->buf,presentNumber);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("已经跟%s添加了好友%s了。\n",presentNumber,tmpbuf->buf);
                        } 
                        //好友添加
                        memset(sql,0,256);
                        sprintf(sql,"INSERT INTO ACCNUM_%s_friend(FRIEND_AccountNumber) VALUES ('%s')",presentNumber,tmpbuf->buf);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("已经跟%s添加了好友%s了。\n",presentNumber,tmpbuf->buf);
                        }                                                                        
                    }
                    else if(tmpbuf->choice==NO)//用户拒绝添加好友
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        char sql[256];
                        sprintf(sql,"update ACCNUM_%s_MES set message_state='%s' where sender='%s' and message_type='%s'",presentNumber,SUB,tmpbuf->buf,REQUEST);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("消息已改为拒绝。\n");
                        } 
                        memset(sql,0,sizeof(char)*256); 
                        sprintf(sql,"update ACCNUM_%s_MES set message_state='%s' where sender='%s' and message_type='%s'",tmpbuf->buf,SUB,tmpbuf->buf,REQUEST);  
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("消息已改为拒绝。\n");
                        }                         
                    }
                    else if(tmpbuf->choice==LOGOFF)//退出登录,踢出登录在线
                    {
                        //判断是否在线，在线即删除
                        for(int x=0;x<onUsed;x++)
                        {
                            if(line[x].fd==evtArr[i].data.fd)
                            {
                                for(int y=x;y<onUsed;y++)
                                {
                                    line[y]=line[y+1];
                                }
                                onUsed--;              
                            }
                        }
                    }
                    else if(tmpbuf->choice==YICHA)//将消息改为已经查看
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        char sql[256];
                        //查找未读消息的发送者
                        sprintf(sql,"select sender from ACCNUM_%s_MES where seceiver='%s' and message_type='%s' and message_state='%s' order by Time",presentNumber,presentNumber,INFORM,UNREAD);
                        QUERYRESULT *asdf = query_db(pDB,sql);
                        if(asdf->row!=0)
                        {
                            for(int x=0;x<(asdf->row+1);x++)
                            {
                                if(x!=0)
                                {
                                    memset(sql,0,sizeof(char)*256);
                                    //将未读信息的发送者的消息改为已读
                                    sprintf(sql,"UPDATE ACCNUM_%s_MES SET message_state='%s' WHERE sender='%s' AND message_type='%s' AND message_state='%s'",asdf->ppResult[x],READ,asdf->ppResult[x],INFORM,UNREAD);
                                    if(1 == exec_sql(pDB,sql))
                                    {
                                        printf("将未读信息的发送者的消息改为已读\n");
                                    } 
                                }                               
                            }
                        }
                        memset(sql,0,sizeof(char)*256);
                        //将自己的消息都改为已读
                        sprintf(sql,"update ACCNUM_%s_MES set message_state='%s' where message_type='%s' and  message_state='%s'",presentNumber,READ,INFORM,UNREAD);  //自己消息改为已查看
                        if(1 == exec_sql(pDB,sql))
                        {
                            printf("将自己的消息都改为已读\n");
                        } 

                    }
                    else if(tmpbuf->choice==ALONEJIA)//进入聊天，添加进聊天结构体数组
                    {
                        ONLINE dandu;
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        dandu.fd=evtArr[i].data.fd;
                        strcpy(dandu.zh,presentNumber);
                        //添加进入聊天结构体
                        lineChat[lineSize++]=dandu;
                    }
                    else if(tmpbuf->choice==ALONETUI) //退出聊天，将其从聊天结构体数组删除
                    {
                        for(int n=0;n<lineSize;n++)
                        {
                            //找到即删
                            if(lineChat[n].fd==evtArr[i].data.fd)
                            {
                                for(int k=n;k<lineSize;k++)
                                {
                                    lineChat[k]=lineChat[k+1];
                                }
                                lineSize--;              
                            }
                        }
                    }
                    else if(tmpbuf->choice==ALONECHAT)//离线,在线聊天
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);
                        FASONG *fasongtmp=(FASONG*)tmpbuf->buf;
                        int m=0;
                        for(int x=0;x<lineSize;x++)
                        {
                            //找到发送对象，在线聊天
                            if(!strcmp(lineChat[x].zh,fasongtmp->haoyou))
                            {
                                m=1;
                                strcpy(fasongtmp->ziji,lineChat[x].zh);
                                strcpy(fasongtmp->haoyou,presentNumber);
                                write(lineChat[x].fd,tmpbuf,sizeof(BUF));
                                char sql[256];
                                sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','%s','%s','%s')",lineChat[x].zh,lineChat[x].zh,presentNumber,fasongtmp->neirong,INFORM,READ);                         
                                if(1 == exec_sql(pDB,sql))
                                {
                                    printf("信息已经接收。\n");
                                }     
                                memset(sql,0,sizeof(char)*256);
                                sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','%s','%s','%s')",presentNumber,lineChat[x].zh,presentNumber,fasongtmp->neirong,INFORM,READ);                         
                                if(1 == exec_sql(pDB,sql))
                                {
                                    printf("信息已经发送。\n");
                                }
                                //处理完成后及时退出   
                                break;                            
                            }
                        }
                        //为找到，离线聊天
                        if(m==0)
                        {
                            char sql[256];
                            sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','%s','%s','%s')",fasongtmp->haoyou,fasongtmp->haoyou,presentNumber,fasongtmp->neirong,INFORM,UNREAD);                         
                            if(1 == exec_sql(pDB,sql))
                            {
                                printf("信息已离线接收。\n");
                            } 
                            memset(sql,0,sizeof(char)*256);    
                            sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','%s','%s','%s')",presentNumber,fasongtmp->haoyou,presentNumber,fasongtmp->neirong,INFORM,UNREAD);                         
                            if(1 == exec_sql(pDB,sql))
                            {
                                printf("信息已离线发送。\n");
                            }   
                        }
                    }
                    else if(tmpbuf->choice==CHAT_GROUP)//群聊
                    {
                        //当前账号
                        char presentNumber[20];
                        //获取当前用户账号
                        lineAccountText(line,onUsed,presentNumber,evtArr[i].data.fd);  
                        printf("当前账号=%s\n",presentNumber);
                        FASONG fasongtmp;
                        strcpy(fasongtmp.neirong,tmpbuf->buf);
                        strcpy(fasongtmp.haoyou,presentNumber);
                        printf("sdfas%s\n",fasongtmp.haoyou);
                        printf("asdfasd%s\n",fasongtmp.neirong);
                        memcpy(tmpbuf->buf,&fasongtmp,sizeof(FASONG));
                        printf("linsize=%d\n",lineSize);
                        for(int x=0;x<lineSize;x++)
                        {
                            //不是自己就发送
                            if(strcmp(lineChat[x].zh,presentNumber))
                            {
                                write(lineChat[x].fd,tmpbuf,sizeof(BUF));
                                char sql[256];
                                sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','%s','%s','%s','%s')",lineChat[x].zh,lineChat[x].zh,presentNumber,fasongtmp.neirong,GROUP,READ);                         
                                if(1 == exec_sql(pDB,sql))
                                {
                                    printf("信息已经接收。\n");
                                }     
                                memset(sql,0,sizeof(char)*256);
                                sprintf(sql,"INSERT INTO ACCNUM_%s_MES (seceiver,sender,content,message_type,message_state) VALUES ('%s','group chat','%s','%s','%s')",presentNumber,presentNumber,fasongtmp.neirong,GROUP,READ);                         
                                if(1 == exec_sql(pDB,sql))
                                {
                                    printf("信息已经发送。\n");
                                }                        
                            }
                        }                                              
                    }                
                }
            }
        }
    }
}